<?php

namespace frontend\models;

use yii\behaviors\TimestampBehavior;
use yii\filters\RateLimitInterface;
use yii\web\IdentityInterface;
use Yii;

/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property integer $nick_name
 * @property integer $ip
 * @property integer $status
 * @property integer $mobile
 * @property integer $referer
 * @property integer $beizhu

 */
class User extends \yii\db\ActiveRecord
{

    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
            ]
        ];

    }


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            [['mobile', 'nick_name'], 'required'],
            [['mobile'], 'match', 'pattern' => '/^1[3456789]\d{9}/'],
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
            ['referer', 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',

            'mobile' => '手机号',

            'status' => '用户状态',
//            'access_token' => '认证key',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
//            'expired_at' => '过期时间',
        ];
    }

    /**
     * @param $smsCode
     * @param $mobile
     * @return array|null|\yii\db\ActiveRecord
     */
    public function validateSmscode($smsCode, $mobile)
    {
        return Sms::find()
            ->where(['mobile' => $mobile, 'code' => $smsCode, 'status' => 0])
            ->andWhere(['>', 'expired_at', time()])
            ->one();
    }

}
